#include <iostream>
#include <vector>
#include <string>

// 定义广告类
class Advertisement {
public:
    int quantity;
    std::string name;
    std::string email;
    // 添加其他属性，如开拍时间、关闭时间等

    Advertisement(int quantity, const std::string& name, const std::string& email)
        : quantity(quantity), name(name), email(email) {}
};

// 归并排序实现
void merge(std::vector<Advertisement>& ads, int left, int mid, int right) {
    int i = left;
    int j = mid + 1;
    std::vector<Advertisement> merged;
    
    // 合并两个有序数组
    while (i <= mid && j <= right) {
        if (ads[i].name <= ads[j].name) {
            merged.push_back(ads[i]);
            i++;
        } else {
            merged.push_back(ads[j]);
            j++;
        }
    }
    
    // 将剩余的元素添加到合并数组中
    while (i <= mid) {
        merged.push_back(ads[i]);
        i++;
    }
    while (j <= right) {
        merged.push_back(ads[j]);
        j++;
    }
    
    // 将合并后的数组复制回原数组
    for (int k = left; k <= right; k++) {
        ads[k] = merged[k - left];
    }
}

void mergeSort(std::vector<Advertisement>& ads, int left, int right) {
    if (left >= right) {
        return;
    }
    
    int mid = left + (right - left) / 2;
    
    // 分治递归
    mergeSort(ads, left, mid);
    mergeSort(ads, mid + 1, right);
    
    // 归并
    merge(ads, left, mid, right);
}

int main() {
    std::vector<Advertisement> ads;
    
    std::string sortKey;
    std::cout << "请输入排序关键字：";
    std::cin >> sortKey;
    
    std::string line;
    while (std::getline(std::cin, line)) {
        int quantity;
        std::string name;
        std::string email;
        
        if (line.empty()) {
            continue;
        }
        
        if (line == sortKey) {
            break;
        }
        
        quantity = std::stoi(line);
        std::getline(std::cin, name);
        std::getline(std::cin, email);
        
        Advertisement ad(quantity, name, email);
        ads.push_back(ad);
    }
    
    // 非递归的归并排序
    mergeSort(ads, 0, ads.size() - 1);
    
    // 输出排好序的广告
    for (int i = 0; i < ads.size(); i++) {
        std::cout << i + 1 << std::endl;
        std::cout << ads[i].name << std::endl;
        std::cout << ads[i].quantity << std::endl;
        std::cout << ads[i].email << std::endl;
    }
    
    return 0;
}